﻿/**
 * 房晓亮
 * 20170717
 * 利用SpireDoc来操作Word
 */

using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ZNYBSProject
{
    public static class SpireDoc
    {
        public static FileFormat getdocformat(string docfile)
        {
            string fileextentsion=  Path.GetExtension(docfile);
            FileFormat docformat;
            switch(fileextentsion)
            {
                case ".docx":
                    docformat=FileFormat.Docx;
                    break;
                case ".doc":
                    docformat=FileFormat.Doc;
                    break;
                default:
                    docformat = FileFormat.Auto;
                    break;   
            }
            return docformat;
        }


        public static void MergeSpireDoc(string[] docs,string savepath)
        {
            //Load two documents  
            //Load Document1 and Document2  
            try
            {
                Document mergedoc1 = new Document();
                Section lastSection = mergedoc1.AddSection();
                for (int i = 0; i < docs.Length; i++)
                {

                    Document subdoc = new Document(docs[i]);


                    //遍历doc2的section和段落，将每一个段落添加到doc1的最后一个section
                    foreach (Section section in subdoc.Sections)
                    {
                        foreach (Paragraph paragraph in section.Paragraphs)
                        {
                            lastSection.Paragraphs.Add(paragraph.Clone() as Paragraph);
                        }
                    }
                }

                //保存为新的文档
                mergedoc1.SaveToFile(savepath+@"\合并文档_2.docx", FileFormat.Docx2013);
            }
            catch (Exception ex)
            { }
        }


           
    }
}
